import tkinter as tk
from tkinter import messagebox
import os
from datetime import datetime

INPUT_DIR = "input"
if not os.path.exists(INPUT_DIR):
    os.makedirs(INPUT_DIR)
    print(f" 创建目录: {INPUT_DIR}")

def on_save():
    user_text = entry.get("1.0", tk.END).strip()
    if not user_text:
        messagebox.showwarning("警告", "请输入内容后再保存！")
        return

    timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
    filename = f"input_{timestamp}.txt"
    file_path = os.path.join(INPUT_DIR, filename)

    try:
        with open(file_path, 'w', encoding='utf-8') as f:
            f.write(user_text)
        messagebox.showinfo("成功", f"内容已保存至:\n{file_path}")
        entry.delete("1.0", tk.END)
    except Exception as e:
        messagebox.showerror("错误", f"保存失败: {e}")

def main():
    root = tk.Tk()
    root.title("用户文本输入器")
    root.geometry("600x400")  # 固定窗口大小
    root.resizable(False, False)  # 禁止改变窗口大小

    # 标签
    label = tk.Label(root, text="请输入文本内容：", font=("微软雅黑", 14))
    label.grid(row=0, column=0, pady=10, padx=10, sticky="w")

    # 输入框
    global entry
    entry = tk.Text(root, height=10, width=50, wrap=tk.WORD, font=("微软雅黑", 12))  # 调整高度和宽度
    entry.grid(row=1, column=0, padx=10, pady=10, sticky="nsew")

    # 按钮
    save_button = tk.Button(
        root,
        text="保存",
        command=on_save,
        width=15,
        bg="green",
        fg="white",
        font=("微软雅黑", 12)
    )
    save_button.grid(row=2, column=0, pady=20, padx=10, sticky="e")  # 使用sticky调整按钮位置

    # 设置行和列权重，使输入框随窗口缩放（尽管此窗口不可调整大小）
    root.grid_rowconfigure(1, weight=1)
    root.grid_columnconfigure(0, weight=1)

    root.mainloop()

if __name__ == "__main__":
    main()